package cn.dhnapplets.service.impl;

import cn.dhnapplets.entity.PackageOrder;
import cn.dhnapplets.entity.TbPackageOrderItem;
import cn.dhnapplets.entity.tbOrderItem;
import cn.dhnapplets.mapper.MerchantDataMapper;


import cn.dhnapplets.service.MerchantDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Author 邱文彬
 * @Date 2021/12/1 8:39
 * @Version 1.0
 */


@Service
public class MerchantDataServiceImpl implements MerchantDataService {


    @Autowired
    private MerchantDataMapper merchantMapper;


    @Override
    public PackageOrder getAll(String recycleName) {


        PackageOrder packageOrder = merchantMapper.getAll(recycleName);
        return packageOrder;
    }

    @Override
    public PackageOrder getToday(String recycleName) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime =  formatter.format(getDayBegin(-0));
        String finishTime =  formatter.format(getDayEnd());
        return merchantMapper.getData(recycleName,startTime,finishTime);
    }

    @Override
    public PackageOrder getSevenDays(String recycleName) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime =  formatter.format(getDayBegin(-7));
        String finishTime =  formatter.format(getDayEnd());
        return merchantMapper.getData(recycleName,startTime,finishTime);
    }

    @Override
    public PackageOrder getThirtyDays(String recycleName) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime =  formatter.format(getDayBegin(-30));
        String finishTime =  formatter.format(getDayEnd());
        return merchantMapper.getData(recycleName,startTime,finishTime);
    }

    @Override
    public PackageOrder getOneYear(String recycleName) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime =  formatter.format(getDayBegin(-365));
        String finishTime =  formatter.format(getDayEnd());
        return merchantMapper.getData(recycleName,startTime,finishTime);
    }


    @Override
    public String getOpenId(String openId){
        return merchantMapper.getOpenId(openId);
    }
    public static Date getDayBegin(int num) {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(cal.DATE, num);
        return cal.getTime();
    }

    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }



}
